package com.redis.demo.rabbitmq.Work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.redis.demo.rabbitmq.Constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
/*
* 、
* 在这里两个消费者共同消费同一个队列也就是我们的工作队列模式
* 、
* */
public class Producer {


    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        //设置虚拟机的地址
        factory.setVirtualHost(Constants.VIRTUALHOST);
        Connection connection=factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        for (int i = 0; i < 10; i++) {
            String msg="hello rabbitmq"+i;
            channel.basicPublish("" ,Constants.WORK_QUEUE,null,msg.getBytes());
        }
        Thread.sleep(2000);
        System.out.println("消息发送成功");
        channel.close();
        connection.close();

    }


}
